<?php 
namespace Home\Controller;
use Think\Controller;
class PublishController extends Controller{
//显示上传界面 三种方式的上传方法

	public function index(){
    $user=$_SESSION['username'];
    $this->assign('user',$user);
		$this->display('index');
	}

         //获取二手精品的所传入的表单数据
	    public function ershou(){
    
    if(IS_POST){
      //首先判断是不是POST表单
       $allowedExts = array("gif", "jpeg", "jpg", "png");
    $temp = explode(".", $_FILES["file"]["name"]);

$extension = end($temp);     // 获取文件后缀名
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& in_array($extension, $allowedExts))
  //然后再判断文件格式对不对
{
  if($_FILES['file']['error']>0){
    //再判断文件上传中有没有错
    $error="文件报错";
    switch ($_FILES['userfile']['error']){
        case 1:
            $error.="文件大小超过了php.ini中upload_max_filesize选项限制的值";
            break;
        case 2:
            $error.="文件大小超过了表单中max_file_size选项设定的值";
            break;
        case 3:
            $error.="文件只有部分被上传";
            break;
        case 4:
            $error.="没有文件被上传";
            break;
        case 6:
            $error.="找不到临时文件夹";
            break;
        case 7:
            $error.="文件写入失败"; 
            break;
        default:
            break;
    }
    $this->assign('error',$error);
    $this->display('publish/error');
}
  else
  {
    //最后按着时间戳拼出上传路径
      $path="Public/upload/" . time().".".substr($_FILES["file"]["type"],6);
      // 如果 upload 目录不存在该文件则将文件上传到 upload 目录下
      move_uploaded_file($_FILES["file"]["tmp_name"],$path);
      $kind=I('post.kinds');
      $desc=I('post.desc');
      $price=I('post.price');
      $area=I('post.area');
      $detail=I('post.detail');
      $contract=I('post.contract');
      $url=substr($path,6);
      $user=$_SESSION['username'];
      $ershou=D('Ershou');
  $res=$ershou->ershou_save($kind,$desc,$price,$area,$detail,$contract,$url,$user);
      if($res==1){
        $this->assign('success','上传成功');
          $this->display('publish/success');
      }else{
        $this->assign('error','上传失败');
          $this->display('publish/error');
      }
      
  }
}
else
{
   $this->assign('error','非法的文件格式');
    $this->display('publish/error');
}
      
    }else{
             $this->assign('error','不是post表单');
             $this->display('publish/error');
    }
   
}
//获取求购所传入的表单数据
 public function ask_buy(){
      $kind=I('post.kind');
      $desc=I('post.des');
      $price=I('post.pric');
      $area=I('post.are');
      $detail=I('post.detai');
      $contract=I('post.contrac');
      $user=$_SESSION['username'];
      $createtime=time();

      $check=D('User');
      $res=$check->isExist($user);
      if($res==0){
        $qiugou=D('Qiugou');
        $isSucess=$qiugou->qiugou($kind,$desc,$price,$area,$detail,$contract,$user,$createtime);
           if($isSucess==1){
            $this->assign('success','上传成功');
            $this->display('success');
           }else{
            $this->assign('error','上传失败');
            $this->display('error');
           }
      }

 }
 
 //获取以物换物的所传入的表单数据
 public function exchange(){
         
    if(IS_POST){
       $allowedExts = array("gif", "jpeg", "jpg", "png");
    $temp = explode(".", $_FILES["ex_pic"]["name"]);

$extension = end($temp);     // 获取文件后缀名
if ((($_FILES["ex_pic"]["type"] == "image/gif")
|| ($_FILES["ex_pic"]["type"] == "image/jpeg")
|| ($_FILES["ex_pic"]["type"] == "image/jpg")
|| ($_FILES["ex_pic"]["type"] == "image/pjpeg")
|| ($_FILES["ex_pic"]["type"] == "image/x-png")
|| ($_FILES["ex_pic"]["type"] == "image/png"))
&& in_array($extension, $allowedExts))
{
  if($_FILES['ex_pic']['error']>0){
    $error="文件报错";
    switch ($_FILES['ex_pic']['error']){
        case 1:
            $error.="文件大小超过了php.ini中upload_max_filesize选项限制的值";
            break;
        case 2:
            $error.="文件大小超过了表单中max_file_size选项设定的值";
            break;
        case 3:
            $error.="文件只有部分被上传";
            break;
        case 4:
            $error.="没有文件被上传";
            break;
        case 6:
            $error.="找不到临时文件夹";
            break;
        case 7:
            $error.="文件写入失败"; 
            break;
        default:
            break;
    }
    $this->assign('error',$error);
    $this->display('publish/error');
}
  else
  {
      $path="Public/upload/" . time().".".substr($_FILES["ex_pic"]["type"],6);
      // 如果 upload 目录不存在该文件则将文件上传到 upload 目录下
      move_uploaded_file($_FILES["ex_pic"]["tmp_name"],$path);
      $kind=I('post.ex_kind');
      $desc=I('post.ex_desc');
      $product=I('post.ex_product');
      $area=I('post.ex_area');
      $detail=I('post.ex_detail');
      $contract=I('post.ex_contract');
      $url=$path;
      $user=$_SESSION['username'];
      $exchange=D('Exchange');
      $res=$exchange->jiaohuan($kind,$desc,$product,$area,$detail,$contract,$url,$user);
     if($res==1){
        $this->assign('success','上传成功');
          $this->display('publish/success');
      }else{
        $this->assign('error','上传失败');
          $this->display('publish/error');
      }
      
  }
}
else
{
   $this->assign('error','非法的文件格式');
    $this->display('publish/error');
}
      
    }else{
             $this->assign('error','不是post表单');
             $this->display('publish/error');
    }
 }

}